﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class OrderTime
    {
        public int Add(Chain.Model.OrderTime model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrderTime(");
            builder.Append("OrderTimeCode,OrderMemID,OrderToken,OrderMarchTime,OrderState,OrderStartUserID,OrderEndUserID,OrderCreateTime,OrderShopID,OrderProjectID,OrderRulesID,OrderPredictTime)");
            builder.Append(" values (");
            builder.Append("@OrderTimeCode,@OrderMemID,@OrderToken,@OrderMarchTime,@OrderState,@OrderStartUserID,@OrderEndUserID,@OrderCreateTime,@OrderShopID,@OrderProjectID,@OrderRulesID,@OrderPredictTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderTimeCode", SqlDbType.VarChar, 50), new SqlParameter("@OrderMemID", SqlDbType.Int, 4), new SqlParameter("@OrderToken", SqlDbType.VarChar, 50), new SqlParameter("@OrderMarchTime", SqlDbType.DateTime), new SqlParameter("@OrderState", SqlDbType.Bit, 1), new SqlParameter("@OrderStartUserID", SqlDbType.Int, 4), new SqlParameter("@OrderEndUserID", SqlDbType.Int, 4), new SqlParameter("@OrderCreateTime", SqlDbType.DateTime), new SqlParameter("@OrderShopID", SqlDbType.Int, 4), new SqlParameter("@OrderProjectID", SqlDbType.Int), new SqlParameter("@OrderRulesID", SqlDbType.Int), new SqlParameter("@OrderPredictTime", SqlDbType.Money) };
            cmdParms[0].Value = model.OrderTimeCode;
            cmdParms[1].Value = model.OrderMemID;
            cmdParms[2].Value = model.OrderToken;
            cmdParms[3].Value = model.OrderMarchTime;
            cmdParms[4].Value = model.OrderState;
            cmdParms[5].Value = model.OrderStartUserID;
            cmdParms[6].Value = model.OrderEndUserID;
            cmdParms[7].Value = model.OrderCreateTime;
            cmdParms[8].Value = model.OrderShopID;
            cmdParms[9].Value = model.OrderProjectID;
            cmdParms[10].Value = model.OrderRulesID;
            cmdParms[11].Value = model.OrderPredictTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int OrderTimeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrderTime ");
            builder.Append(" where OrderTimeID=@OrderTimeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderTimeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = OrderTimeID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string OrderTimeIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrderTime ");
            builder.Append(" where OrderTimeID in (" + OrderTimeIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int OrderTimeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from OrderTime");
            builder.Append(" where OrderTimeID=@OrderTimeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderTimeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = OrderTimeID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool ExistsProject(int ProjectID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from OrderTime");
            builder.Append(" where OrderProjectID=@OrderProjectID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderProjectID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProjectID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool ExistsRules(int RulesID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from OrderTime");
            builder.Append(" where OrderRulesID=@OrderRulesID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderRulesID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RulesID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM OrderTime ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" * FROM OrderTime ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.OrderTimeID desc");
            }
            builder.Append(")AS Row, T.*  from OrderTime T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "V_OrderTime";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "OrderCreateTime", "OrderTimeID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("OrderTimeID", "OrderTime");
        }

        public Chain.Model.OrderTime GetModel(int OrderTimeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 * from OrderTime ");
            builder.Append(" where OrderTimeID=@OrderTimeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderTimeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = OrderTimeID;
            Chain.Model.OrderTime time = new Chain.Model.OrderTime();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["OrderTimeID"] != null) && (set.Tables[0].Rows[0]["OrderTimeID"].ToString() != ""))
            {
                time.OrderTimeID = int.Parse(set.Tables[0].Rows[0]["OrderTimeID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderTimeCode"] != null) && (set.Tables[0].Rows[0]["OrderTimeCode"].ToString() != ""))
            {
                time.OrderTimeCode = set.Tables[0].Rows[0]["OrderTimeCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["OrderMemID"] != null) && (set.Tables[0].Rows[0]["OrderMemID"].ToString() != ""))
            {
                time.OrderMemID = int.Parse(set.Tables[0].Rows[0]["OrderMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderToken"] != null) && (set.Tables[0].Rows[0]["OrderToken"].ToString() != ""))
            {
                time.OrderToken = set.Tables[0].Rows[0]["OrderToken"].ToString();
            }
            if ((set.Tables[0].Rows[0]["OrderMarchTime"] != null) && (set.Tables[0].Rows[0]["OrderMarchTime"].ToString() != ""))
            {
                time.OrderMarchTime = DateTime.Parse(set.Tables[0].Rows[0]["OrderMarchTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderState"] != null) && (set.Tables[0].Rows[0]["OrderState"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["OrderState"].ToString() == "1") || (set.Tables[0].Rows[0]["OrderState"].ToString().ToLower() == "true"))
                {
                    time.OrderState = true;
                }
                else
                {
                    time.OrderState = false;
                }
            }
            if ((set.Tables[0].Rows[0]["OrderOutTime"] != null) && (set.Tables[0].Rows[0]["OrderOutTime"].ToString() != ""))
            {
                time.OrderOutTime = DateTime.Parse(set.Tables[0].Rows[0]["OrderOutTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderStartUserID"] != null) && (set.Tables[0].Rows[0]["OrderStartUserID"].ToString() != ""))
            {
                time.OrderStartUserID = int.Parse(set.Tables[0].Rows[0]["OrderStartUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderEndUserID"] != null) && (set.Tables[0].Rows[0]["OrderEndUserID"].ToString() != ""))
            {
                time.OrderEndUserID = int.Parse(set.Tables[0].Rows[0]["OrderEndUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderCreateTime"] != null) && (set.Tables[0].Rows[0]["OrderCreateTime"].ToString() != ""))
            {
                time.OrderCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["OrderCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderShopID"] != null) && (set.Tables[0].Rows[0]["OrderShopID"].ToString() != ""))
            {
                time.OrderShopID = int.Parse(set.Tables[0].Rows[0]["OrderShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderProjectID"] != null) && (set.Tables[0].Rows[0]["OrderProjectID"].ToString() != ""))
            {
                time.OrderProjectID = int.Parse(set.Tables[0].Rows[0]["OrderProjectID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderRulesID"] != null) && (set.Tables[0].Rows[0]["OrderRulesID"].ToString() != ""))
            {
                time.OrderRulesID = int.Parse(set.Tables[0].Rows[0]["OrderRulesID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderRemark"] != null) && (set.Tables[0].Rows[0]["OrderRemark"].ToString() != ""))
            {
                time.OrderRemark = set.Tables[0].Rows[0]["OrderRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["OrderPredictTime"] != null) && (set.Tables[0].Rows[0]["OrderPredictTime"].ToString() != ""))
            {
                time.OrderPredictTime = Convert.ToDecimal(set.Tables[0].Rows[0]["OrderPredictTime"]);
            }
            return time;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM OrderTime ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool isUse(int Project)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from OrderTime");
            builder.Append(" where OrderState=0 and OrderProjectID=@OrderProjectID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderProjectID", SqlDbType.Int, 4) };
            cmdParms[0].Value = Project;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Update(Chain.Model.OrderTime model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrderTime set ");
            builder.Append("OrderTimeCode=@OrderTimeCode,");
            builder.Append("OrderMemID=@OrderMemID,");
            builder.Append("OrderToken=@OrderToken,");
            builder.Append("OrderMarchTime=@OrderMarchTime,");
            builder.Append("OrderState=@OrderState,");
            builder.Append("OrderOutTime=@OrderOutTime,");
            builder.Append("OrderStartUserID=@OrderStartUserID,");
            builder.Append("OrderEndUserID=@OrderEndUserID,");
            builder.Append("OrderCreateTime=@OrderCreateTime,");
            builder.Append("OrderShopID=@OrderShopID,");
            builder.Append("OrderProjectID=@OrderProjectID,");
            builder.Append("OrderRulesID=@OrderRulesID,");
            builder.Append("OrderRemark=@OrderRemark");
            builder.Append(" where OrderTimeID=@OrderTimeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderTimeCode", SqlDbType.VarChar, 50), new SqlParameter("@OrderMemID", SqlDbType.Int, 4), new SqlParameter("@OrderToken", SqlDbType.VarChar, 50), new SqlParameter("@OrderMarchTime", SqlDbType.DateTime), new SqlParameter("@OrderState", SqlDbType.Bit, 1), new SqlParameter("@OrderOutTime", SqlDbType.DateTime), new SqlParameter("@OrderStartUserID", SqlDbType.Int, 4), new SqlParameter("@OrderEndUserID", SqlDbType.Int, 4), new SqlParameter("@OrderCreateTime", SqlDbType.DateTime), new SqlParameter("@OrderShopID", SqlDbType.Int, 4), new SqlParameter("@OrderTimeID", SqlDbType.Int, 4), new SqlParameter("@OrderProjectID", SqlDbType.Int, 4), new SqlParameter("@OrderRulesID", SqlDbType.Int, 4), new SqlParameter("@OrderRemark", SqlDbType.NVarChar, 400) };
            cmdParms[0].Value = model.OrderTimeCode;
            cmdParms[1].Value = model.OrderMemID;
            cmdParms[2].Value = model.OrderToken;
            cmdParms[3].Value = model.OrderMarchTime;
            cmdParms[4].Value = model.OrderState;
            cmdParms[5].Value = model.OrderOutTime;
            cmdParms[6].Value = model.OrderStartUserID;
            cmdParms[7].Value = model.OrderEndUserID;
            cmdParms[8].Value = model.OrderCreateTime;
            cmdParms[9].Value = model.OrderShopID;
            cmdParms[10].Value = model.OrderTimeID;
            cmdParms[11].Value = model.OrderProjectID;
            cmdParms[12].Value = model.OrderRulesID;
            cmdParms[13].Value = model.OrderRemark;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

